<?php
class SanphamsController extends AppController {

	var $name = 'Sanphams';

	function index($loai=null) {
		$this->Sanpham->recursive = 0;
		$this->set('sanphams', $this->paginate(array('hanghoa_id'=>$loai)));
        $ten=$this->Sanpham->query('select tenhh from hanghoas where id='.$loai);
        $this->set(compact('loai','ten'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid sanpham', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('sanpham', $this->Sanpham->read(null, $id));
	}

	function add($loai) {
		if (!empty($this->data)) {
			$this->Sanpham->create();
			if ($this->Sanpham->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true));
				$this->redirect(array('action' => 'index',$loai));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu.', true));
			}
		}
		$hanghoas = $this->Sanpham->Hanghoa->find('list');
        $ten=$this->Sanpham->query('select tenhh from hanghoas where id='.$loai);
		$this->set(compact('hanghoas','loai','ten'));
	}

	function edit($id = null,$loai=null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid sanpham', true));
			$this->redirect(array('action' => 'index',$loai));
		}
		if (!empty($this->data)) {
			if ($this->Sanpham->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true));
				$this->redirect(array('action' => 'index',$this->data['Sanpham']['hanghoa_id']));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Sanpham->read(null, $id);
		}
		$hanghoas = $this->Sanpham->Hanghoa->find('list');
		$this->set(compact('hanghoas','loai'));
	}

	function delete($id = null,$loai=null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for sanpham', true));
			$this->redirect(array('action'=>'index',$loai));
		}
		if ($this->Sanpham->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true));
			$this->redirect(array('action'=>'index',$loai));
		}
		$this->Session->setFlash(__('Thông tin không thể xóa', true));
		$this->redirect(array('action' => 'index',$loai));
	}
}
